package com.wuxiaofan.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.entity.HospitalSet;
import com.wuxiaofan.entity.custom.HospitalSetCustom;
import com.wuxiaofan.entity.model.HospitalSetMongo;
import com.wuxiaofan.entity.vo.HospitalQueryVo;
import com.wuxiaofan.service.HospitalSetService;
import com.wuxiaofan.vo.ReturnResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;

/**
 * @author wuxiaofan
 */
@RestController
@Slf4j
@Api(tags="医院设置表")
@RequestMapping("/hospitalSet")
public class HospitalSetApi {


    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("根据id查询医院设置")
    @GetMapping("/findById/{id}")
    public ReturnResult findById(@PathVariable("id") String id){
        HospitalSet hospitalSet = hospitalSetService.findById(id);
        return ReturnResult.success("",hospitalSet);
    }

    @ApiOperation("查询所有医院设置")
    @GetMapping("findAll")
    public ReturnResult findAll(){
        Map paraMap = new HashMap();
        List<HospitalSetCustom> hospitalSetCustoms = hospitalSetService.findByMap(paraMap);
        return ReturnResult.success("",hospitalSetCustoms);
    }

    @PostMapping("list")
    public ReturnResult list(@RequestBody JSONObject jsonObject){
        Page page = new Page(jsonObject.getIntValue("current"),jsonObject.getIntValue("size"));
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        IPage<HospitalSetCustom> hospitalSetCustoms = hospitalSetService.findByMap(paraMap,page);
        return ReturnResult.success("",hospitalSetCustoms);
    }

    @PostMapping("findByPage")
    public ReturnResult findByPage(@RequestBody HospitalQueryVo hospitalQueryVo){
        Page page = new Page(hospitalQueryVo.getCurrent(),hospitalQueryVo.getSize());
        Map paraMap = JSONObject.parseObject(JSONObject.toJSONString(hospitalQueryVo),Map.class);
        IPage<HospitalSetCustom> hospitalSetCustoms = hospitalSetService.findByMap(paraMap,page);
        return ReturnResult.success("",hospitalSetCustoms);
    }

    @PostMapping("saveMongo")
    public ReturnResult saveMongo(@RequestBody HospitalSetMongo hospitalSetMongo){
        hospitalSetService.save(hospitalSetMongo);
        return ReturnResult.success("保存成功");
    }

    @PostMapping("saveOrUpdate")
    public ReturnResult saveOrUpdate(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.saveOrUpdate(hospitalSet);
        return ReturnResult.success("保存成功");
    }

    @PostMapping("update")
    public ReturnResult update(@RequestBody JSONObject jsonObject){
        Map paraMap = jsonObject;
        hospitalSetService.updateMapById(paraMap);
        return ReturnResult.success("更新成功");
    }

    @DeleteMapping("delById/{id}")
    public ReturnResult delById(@PathVariable("id") String id){
        hospitalSetService.delById(id);
        return ReturnResult.success("删除成功");
    }

    @PostMapping("batchRemove")
    public ReturnResult batchRemove(@RequestBody List<String> ids){
        log.info("批量删除id:{}",ids);
        hospitalSetService.batchRemove(ids);
        return ReturnResult.success("删除成功");
    }

    @ApiOperation(value = "获取医院信息")
    @GetMapping("hospital/show")
    public ReturnResult hospital(String hoscode) {
        return ReturnResult.success("查询成功",hospitalSetService.getByHoscode(hoscode));
    }

}
